<!DOCTYPE html>
<html lang="zh">
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<title>并联电路计算</title>
		<style>
			label{
				font-size: 20px;
			}
			input[type = "number"],input[type = "text"]{
				font-size: 18px;
				width: 200px;
				height: 20px;
				padding: 5px;
			}
			button{
				font-size: 20px;
			}
			#result{
				font-size: 18px;
			}
		</style>
	</head>
	<body>
		<h1>并联电路电阻计算</h1>
		
		<label for="totalResistance">总电阻 (Ω):</label>
		<br>
		<input type="number" id="totalResistance" placeholder="请输入总电阻">
		<br>
		
		<label for="resistorValues">小电阻阻值 (逗号分隔):</label>
		<br>
		<input type="text" id="resistorValues" placeholder="请输入小电阻阻值">
		<br>
		
		<label for="maxCount">最大电阻数量:</label>
		<br>
		<input type="number" id="maxCount" placeholder="请输入最大电阻数量">
		<br><br>
		
		<button onclick="calculateParallel()">计算并联电路</button>
		<button onclick="goBack()">返回首页</button>
		<br>
		
		<div id="result"></div>
    <script>
        // 页面加载时，从localStorage中获取之前的输入数据
        window.onload = function() {
            const totalResistance = localStorage.getItem("parallelTotalResistance");
            const resistorValues = localStorage.getItem("parallelResistorValues");
            const maxCount = localStorage.getItem("parallelMaxCount");

            if (totalResistance) {
                document.getElementById("totalResistance").value = totalResistance;
            }
            if (resistorValues) {
                document.getElementById("resistorValues").value = resistorValues;
            }
            if (maxCount) {
                document.getElementById("maxCount").value = maxCount;
            }
        };

        // 实时保存用户输入的数据
        document.getElementById("totalResistance").addEventListener("input", function() {
            localStorage.setItem("parallelTotalResistance", this.value);
        });
        document.getElementById("resistorValues").addEventListener("input", function() {
            localStorage.setItem("parallelResistorValues", this.value);
        });
        document.getElementById("maxCount").addEventListener("input", function() {
            localStorage.setItem("parallelMaxCount", this.value);
        });

        // 计算并联电路
        function calculateParallel() {
            const totalResistance = parseFloat(document.getElementById("totalResistance").value);
            const resistorValuesStr = document.getElementById("resistorValues").value;
            const maxCount = parseInt(document.getElementById("maxCount").value);

            if (isNaN(totalResistance) || !resistorValuesStr || isNaN(maxCount)) {
                document.getElementById("result").innerText = "请填写所有字段！";
                return;
            }

            const resistorValues = resistorValuesStr.replace(/，/g, ',').split(',').map(value => parseFloat(value.trim()));

            if (resistorValues.some(isNaN)) {
                document.getElementById("result").innerText = "小电阻的格式不正确！";
                return;
            }

            // 查找组合，允许小电阻重复使用
            const combinations = findParallelCombinationsWithRepetition(resistorValues, maxCount, totalResistance);

            if (combinations.length > 0) {
                document.getElementById("result").innerText = "找到的组合:\n" + combinations.map(comb => comb.join(' + ') + " = " + totalResistance + " Ω").join("\n");
            } else {
                document.getElementById("result").innerText = "没有找到合适的电阻组合！";
            }
        }

        // 查找组合函数 - 允许小电阻重复使用且去除重复组合
        function findParallelCombinationsWithRepetition(arr, maxCount, targetResistance) {
            const results = [];
            const uniqueCombinations = new Set();

            function calculateEquivalentResistance(combo) {
                const reciprocalSum = combo.reduce((sum, resistor) => sum + 1 / resistor, 0);
                return reciprocalSum === 0 ? Infinity : 1 / reciprocalSum;
            }

            function combine(currentCombo) {
                if (currentCombo.length > 0 && currentCombo.length <= maxCount) {
                    const eqResistance = calculateEquivalentResistance(currentCombo);
                    if (Math.abs(eqResistance - targetResistance) < 1e-6) {
                        const sortedCombo = [...currentCombo].sort((a, b) => a - b).join(',');
                        if (!uniqueCombinations.has(sortedCombo)) {
                            uniqueCombinations.add(sortedCombo);
                            results.push([...currentCombo]);
                        }
                        return;
                    }
                }
                if (currentCombo.length >= maxCount) return;

                for (let i = 0; i < arr.length; i++) {
                    currentCombo.push(arr[i]);
                    combine(currentCombo);
                    currentCombo.pop();
                }
            }
            combine([]);
            return results;
        }
		// 返回首页
		function goBack() {
		    window.location.href = "index.html";
		}
    </script>
	</body>
</html>
